package com.colorfruit.admin.business.service.impl;

import com.colorfruit.admin.business.dao.FoundationPickGoodsAddressMapper;
import com.colorfruit.admin.business.dao.FoundationUserReceiveAddressMapper;
import com.colorfruit.admin.business.service.AddressService;
import com.colorfruit.admin.pojo.constant.CommonConstant;
import com.colorfruit.admin.pojo.entity.FoundationPickGoodsAddress;
import com.colorfruit.admin.pojo.entity.FoundationUserReceiveAddress;
import com.colorfruit.admin.pojo.enums.ErrorCodeEnum;
import com.colorfruit.admin.pojo.enums.StatusEnum;
import com.colorfruit.framework.common.util.DataValidateUtil;
import com.colorfruit.framework.model.pojo.BaseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    FoundationUserReceiveAddressMapper foundationUserReceiveAddressMapper; //用户收件地址

    @Autowired
    FoundationPickGoodsAddressMapper foundationPickGoodsAddressMapper; //取货点地址

    @Override
    public BaseResult addUserReceiveAddress(FoundationUserReceiveAddress foundationUserReceiveAddress) {
        try {
            log.info("[service] [IN] addUserReceiveAddress  param : foundationUserReceiveAddress = {}", foundationUserReceiveAddress.toString() );
            if (!DataValidateUtil.verifyString(foundationUserReceiveAddress.getProvinceName(),
                    foundationUserReceiveAddress.getCityName(),
                    foundationUserReceiveAddress.getAreaName(),
                    foundationUserReceiveAddress.getAddressDescription())){
                return new BaseResult(ErrorCodeEnum.DATA_ERROR.getCode(), "省,市,区,地址详情不能为空");
            }
            Integer addressNum  = foundationUserReceiveAddressMapper.countAddressNum(foundationUserReceiveAddress.getUserId());
            if (addressNum >= CommonConstant.ADDRESS_LIMIT_NUM){
                return new BaseResult(ErrorCodeEnum.DATA_ERROR.getCode(), "您的地址已经满了");
            }
            foundationUserReceiveAddress.setRecordStatus(StatusEnum.EFFECTIVE_STATUS.getStatusCode());
            foundationUserReceiveAddress.setCreateTime(new Date());
            foundationUserReceiveAddress.setUpdateTime(new Date());
            if (foundationUserReceiveAddress.getIsDefault() == (byte)2){
                foundationUserReceiveAddressMapper.updateAddressNotDefault(foundationUserReceiveAddress.getUserId());
            }
            foundationUserReceiveAddressMapper.insertSelective(foundationUserReceiveAddress);
            log.info("[service] [OUT] addUserReceiveAddress success,  ");
            return new BaseResult(ErrorCodeEnum.SUCCESS);
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            throw e;
        }
    }

    @Override
    public BaseResult editUserReceiveAddress(FoundationUserReceiveAddress foundationUserReceiveAddress) {
        log.info("[service] [IN] editUserReceiveAddress  param : foundationUserReceiveAddress = {}", foundationUserReceiveAddress.toString() );
        if (foundationUserReceiveAddress.getIsDefault() == (byte)2){
            foundationUserReceiveAddressMapper.updateAddressNotDefault(foundationUserReceiveAddress.getUserId());
        }
        foundationUserReceiveAddress.setRecordStatus((byte)1);
        foundationUserReceiveAddressMapper.updateByPrimaryKey(foundationUserReceiveAddress);
        log.info("[service] [OUT] editUserReceiveAddress success,  ");
        return new BaseResult(ErrorCodeEnum.SUCCESS);
    }

    @Override
    public List<FoundationUserReceiveAddress> selectUserAddressList(String userId) {
        try {
            log.info("[service] [IN] selectUserAddressList  param : userId = {}", userId );
            List<FoundationUserReceiveAddress> foundationUserReceiveAddressesList = foundationUserReceiveAddressMapper.selectUserAddressList(userId);
            log.info("[service] [OUT] selectUserAddressList success,size = {}  ", foundationUserReceiveAddressesList.size());
            return foundationUserReceiveAddressesList;
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            throw e;
        }
    }

    @Override
    public BaseResult deleteUserReceiveAddress(String userId, Long addressId) {
        try {
            log.info("[service] [IN] deleteUserReceiveAddress  param : userId = {}, addressId = {}", userId, addressId );
            FoundationUserReceiveAddress foundationUserReceiveAddress = foundationUserReceiveAddressMapper.selectByPrimaryKey(addressId);
            if (foundationUserReceiveAddress == null){
                return new BaseResult("地址ID跟用户ID不匹配");
            }
            foundationUserReceiveAddressMapper.updateUserAddressStatus(addressId, StatusEnum.CANCELLATION_STATUS.getStatusCode());
            log.info("[service] [OUT] deleteUserReceiveAddress success,  ");
            return new BaseResult(ErrorCodeEnum.SUCCESS);
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            throw e;
        }
    }

    @Override
    public FoundationUserReceiveAddress getDefaultAddress(String userId) {
        try {
            log.info("[service] [IN] getDefaultAddress  param : userId = {}", userId );
            FoundationUserReceiveAddress defaultAddress = foundationUserReceiveAddressMapper.getDefaultAddress(userId);
            log.info("[service] [OUT] getDefaultAddress success,  ");
            return defaultAddress;
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            throw e;
        }
    }

    @Override
    public BaseResult setDefaultAddress(String userId, Long addressId) {
        try {
            log.info("[service] [IN] getDefaultAddress  param : userId = {}, addressId = {}", userId , addressId);
            foundationUserReceiveAddressMapper.updateAddressNotDefault(userId);
            foundationUserReceiveAddressMapper.setDefaultAddress(addressId);
            log.info("[service] [OUT] getDefaultAddress success,  ");
            return new BaseResult(ErrorCodeEnum.SUCCESS);
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            throw e;
        }
    }

    @Override
    public List<FoundationPickGoodsAddress> selectPickGoodsAddressList() {
        try {
            log.info("[service] [IN] selectPickGoodsAddressList  " );
            List<FoundationPickGoodsAddress> foundationPickGoodsAddresses = foundationPickGoodsAddressMapper.selectPickGoodsAddressList();
            log.info("[service] [OUT] selectPickGoodsAddressList success,  ");
            return foundationPickGoodsAddresses;
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            throw e;
        }
    }

    @Override
    public FoundationPickGoodsAddress getPickGoodsAddress(Long id) {
        try {
            log.info("[service] [IN] getPickGoodsAddress  " );
            FoundationPickGoodsAddress foundationPickGoodsAddresses = foundationPickGoodsAddressMapper.selectByPrimaryKey(id);
            log.info("[service] [OUT] getPickGoodsAddress success,  ");
            return foundationPickGoodsAddresses;
        }catch (Exception e){
            log.error("# [OUT] errorMessage: " + e.getMessage(), e);
            throw e;
        }
    }
}
